package com.orhonit.ipb.portal.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.task.Comment;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.ag.core.context.BaseContextHandler;
import com.orhonit.ipb.portal.biz.GropToDeptBiz;
import com.orhonit.ipb.portal.biz.JoApplyJoinPartyBiz;
import com.orhonit.ipb.portal.biz.TbPartyOrgCatalogBiz;
import com.orhonit.ipb.portal.biz.WorkflowServer;
import com.orhonit.ipb.portal.entity.JoApplyJoinParty;
import com.orhonit.ipb.security.auth.client.annotation.CheckUserToken;

/**
 * @author Created by yawn on 2018-01-07 20:21
 */
@RestController
@RequestMapping("JoinPartyController")
public class JoinPartyController {
    @Autowired
    private WorkflowServer workflowServer;
    @Autowired
    private JoApplyJoinPartyBiz joApplyJoinPartyBiz;
    @Autowired
    private TbPartyOrgCatalogBiz tbPartyOrgCatalogBiz;
    @Autowired
    private GropToDeptBiz gropToDeptBiz;
    @Resource
    private RepositoryService repositoryService;
	/**
	 * 开始流程 
	 * @return
	 */
	@RequestMapping(value="/startProcess",method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<?> startProcess(){
		workflowServer.startVac("14","32133");
		return ResponseEntity.ok(null);
	}
	/**
	 * 申请人
	 * @return
	 */
	@RequestMapping(value="/findTaskListByName",method=RequestMethod.GET)
	@CheckUserToken
	public ResponseEntity<?> findTaskListByName(){
		String userId =BaseContextHandler.getUserID();
//		String userId="89c75217-86af-4e62-bc11-37eb63bd273f";
		List<JoApplyJoinParty> list=workflowServer.findTaskListByName(userId);
		return ResponseEntity.ok().body(list);
	}
	/**
	 * 支部查询待办任务
	 * @return
	 */
	@RequestMapping(value="/findTaskListByDeptId",method=RequestMethod.GET)
	@ResponseBody
	@CheckUserToken
	public ResponseEntity<?> findTaskListByDeptId(){
		String userId =BaseContextHandler.getUserID();
		String deptId=gropToDeptBiz.getdeptId(userId).getDeptId();
		/*List<String> deptIds= gropToDeptBiz.getDeptList(userId);//根据用户ID拿到管理的组织
		if(deptIds.size() == 0){
			return ResponseEntity.ok(null);
		}
		Set<String> allDeptIds = tbPartyOrgCatalogBiz.getAllDeptList(deptIds);//查询了所有该用户下需要查看的组织列表
		List<String> result = new ArrayList<>(allDeptIds);
		List<JoApplyJoinParty> list=new ArrayList<JoApplyJoinParty>();
		for(int i=0;i<result.size();i++){
			List<JoApplyJoinParty> list1=workflowServer.findTaskListByName(result.get(i));
			for(int j=0;j<list1.size();j++){
				list.add(list1.get(j));
			}D:\oracle\app\oracle\product\11.2.0\server\bin;%CATALINA_HOME%\bin;%CATALINA_HOME%\lib;C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;D:\study\java\eclipse\bin;D:\study\mysql\MySQL Utilities 1.4.3\;D:\study\mysql\MySQL Utilities 1.4.3\Doctrine extensions for PHP\;D:\study\python;D:\oracle\app\oracle\product\11.2.0\server;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;D:\study\Git\Git\cmd;E:\orhon\node\nodejs\;E:\orhon\svn\svn\bin;D:\study\python\Scripts;%M2_HOME%\bin
		}*/
		List<JoApplyJoinParty> list=workflowServer.findTaskListByName(deptId);
		return ResponseEntity.ok().body(list);
	}
	
	/**
	 * 通过任务id查询申请单信息
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value="/findApplyByTaskId",method=RequestMethod.GET)
	@ResponseBody
	public  ResponseEntity<?> findApplyByTaskId(String taskId){
		JoApplyJoinParty joApplyJoinParty=workflowServer.findApplyByTaskId(taskId);
		return ResponseEntity.ok().body(joApplyJoinParty);
	} 
	
	
	/**
	 * 提交任务
	 * @param taskId 任务 id
	 * @param outcome 连线名称 00 10:拒绝 10 11：同意
	 * @param message 附带信息 当前状态名称 如：递交入党申请书
	 * @param id 业务id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/submitTask",method=RequestMethod.POST)
	@CheckUserToken
	public ResponseEntity<?> submitTask(String taskId,String outcome,String message,String id){
		String userId =BaseContextHandler.getUserID();
//		String userId="89c75217-86af-4e62-bc11-37eb63bd273f";
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("taskId", taskId);
		map.put("outcome", outcome);
		map.put("message", message);
		JoApplyJoinParty joApplyJoinParty=joApplyJoinPartyBiz.getJoApplyJoinPartyByid(id);
		if(userId.equals(joApplyJoinParty.getUserId())){
			map.put("user", BaseContextHandler.getName());
//			map.put("user", "申请人");
		}
		else{
			String deptname=tbPartyOrgCatalogBiz.getDeptNameByDeptId(userId);
//			String deptname="支部";
			map.put("user", deptname);
		}
		workflowServer.saveSubmitTask(map);
		return ResponseEntity.ok(null);
	}
	@ResponseBody
	@RequestMapping(value="/test",method=RequestMethod.POST)
	@CheckUserToken
	public ResponseEntity<?> deptname(){
		System.out.println("dsad");
		String id=BaseContextHandler.getUserID();
		String deptname=tbPartyOrgCatalogBiz.getDeptNameByDeptId(id);
		return ResponseEntity.ok(deptname);
	}
	/**
	 * 申请人已办理任务
	 * @return
	 */
	@RequestMapping(value="/findHistoryListByName",method=RequestMethod.GET)
	@ResponseBody
	@CheckUserToken
	public ResponseEntity<?> findHistoryListByName(){
		String userId =BaseContextHandler.getUserID();
//		String userId ="89c75217-86af-4e62-bc11-37eb63bd273f";
		List<JoApplyJoinParty> list=workflowServer.findHistoryListByName(userId);
		return ResponseEntity.ok().body(list);
	}
	/**
	 * 支部查询以办任务
	 * @return
	 */
	@RequestMapping(value="/findHistoryListByDeptId",method=RequestMethod.GET)
	@ResponseBody
	@CheckUserToken
	public ResponseEntity<?> findHistoryListByDeptId(){
		String userId =BaseContextHandler.getUserID();
		String deptId=gropToDeptBiz.getdeptId(userId).getDeptId();
		List<JoApplyJoinParty> list1=joApplyJoinPartyBiz.getJoApplyJoinPartyuserBydeptId(deptId);
		List<JoApplyJoinParty> all=new ArrayList<JoApplyJoinParty>();
		if(list1.size()>0){
			for(int i=0;i<list1.size();i++){
				List<JoApplyJoinParty> list=workflowServer.findHistoryListByName(list1.get(i).getUserId());
				for(int j=0;j<list.size();j++){
					all.add(list.get(j));
				}
			}
			return ResponseEntity.ok().body(all);
		}
		else{
			return ResponseEntity.ok().body(null);
		}
	}
	/**
	 * 通过申请id查询历史批注
	 * @param applyId 申请id
	 * @return
	 */
	@RequestMapping(value="/findCommentByapplyId",method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<?> findCommentByapplyId(String applyId){
		List<Comment> list=workflowServer.findCommentByapplyId(applyId);
		return ResponseEntity.ok().body(list);
	}
	/**
	 * @Author liuyang
	 * @Note 读取流程资源
	 * @Date 2018-5-25 16:02:09
	 * @param processDefinitionId 流程定义ID
	 * @param pProcessInstanceId  流程ID
	 */
	@RequestMapping(value = "/viewCurrentImage",method=RequestMethod.GET)
	public void viewCurrentImage(String processDefinitionId ,String processInstanceId, HttpServletResponse response)
	        throws Exception {
	    // 设置页面不缓存
	    response.setHeader("Pragma", "No-cache");
	    response.setHeader("Cache-Control", "no-cache");
	    response.setDateHeader("Expires", 0);
	    ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery();
	    ProcessDefinition pd = pdq.processDefinitionId(processDefinitionId).singleResult();
	    System.out.println(StringUtils.isEmpty(processInstanceId));
	    workflowServer.getActivitiProccessImage(processInstanceId,response);
	}
	/**
	 * 区分组织权限
	 * @param JoinId
	 * @return
	 */
	@RequestMapping(value="/Jurisdiction",method=RequestMethod.GET)
	@CheckUserToken
	public ResponseEntity<?> Jurisdiction(String JoinId){
		String userId =BaseContextHandler.getUserID();
		String deptID =gropToDeptBiz.getdeptId(userId).getDeptId();
		JoApplyJoinParty temp=joApplyJoinPartyBiz.getJoApplyJoinPartyByIDd(JoinId);
		if(deptID.equals(temp.getParentUserId())){
			return ResponseEntity.ok().body(1);
		}
		else if(deptID.equals(temp.getUpdeptid())){
			return ResponseEntity.ok().body(2);
		}
		else if(deptID.equals(temp.getUpupdeptid())){
			return ResponseEntity.ok().body(3);
		}
		else{
			return ResponseEntity.ok().body(null);
		}
	}
}
